<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common/common_head :: commonHeader('报表更新')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
</head>
<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="form-group">
            <div class="col-sm-8" style="margin-left: 16px">
                <input class="btn btn-primary" type="button" onclick="saveReport()" value="保存">
                <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
            </div>
        </div>
        <form class="ibox-content form-horizontal" method="post" id="reportFrom" enctype="multipart/form-data">
            <table class="layui-table" style="width:68%;margin: 0 8%;">
                <input id="pkId" name="pkId" type="text" th:value="${report.pkId}" hidden>
                <thead style="height:100px">
                <tr>
                    <th colspan="4" style="font-size: 29px;text-align: center;line-height: 50px">RoHS报告更新</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td width="17%" height="60px">原检测机构：</td>
                    <td width="33%" id="originalTestingInstitution" th:text="${report.testingInstitution}"></td>
                    <td>原测试报告编号:</td>
                    <td id="originalReportCode" th:text="${report.reportCode}"></td>
                </tr>
                <tr>
                    <td height="60px">原第三方报告委托单位：</td>
                    <td id="originalConsignee" th:text="${report.consignee}"></td>
                    <td>原附件：</td>
                    <td id="originalAttach"><a href="#" onclick="dowloadFileById(this)" th:value="${report.pkId}"
                                               th:text="${report.attach}" style="color:blue;line-height: 3em;"> </a>
                    </td>
                </tr>
                <tr>
                    <td height="60px">原报告日期：</td>
                    <td id="originalReportDate" th:text="${#dates.format(report.reportDate,'yyyy-MM-dd')}"></td>
                    <td>原有效日期至：</td>
                    <td id="originalExpireDate" th:text="${#dates.format(report.expireDate,'yyyy-MM-dd')}"></td>
                </tr>
                <tr>
                    <td height="80px"><span class="required-sign">*</span>检测机构：</td>
                    <td><input type="text" required class="form-control" name="testingInstitution"/></td>
                    <td><span class="required-sign">*</span>测试报告编号：</td>
                    <td><input id="reportCode" name="reportCode" type="text" required class="form-control"
                               autocomplete="off"/></td>
                </tr>
                <tr>
                    <td height="60px"><span class="required-sign">*</span>第三方报告委托单位：</td>
                    <td><input name="consignee" type="text" required class="form-control"/></td>
                    <td><span class="required-sign">*</span>附件：</td>
                    <td><input id="attach" type="file" name="attachFile" required/></td>
                </tr>
                <tr>
                    <td height="60px"><span class="required-sign">*</span>报告日期：</td>
                    <td><input id="reportDate" name="reportDate" type="text" required autocomplete="off"
                               class="form-control"/></td>
                    <td><span class="required-sign">*</span>有效日期至：</td>
                    <td><input id="expireDate" name="expireDate" type="text" required autocomplete="off"
                               class="form-control"/></td>
                </tr>
                </tbody>
            </table>
        </form>
    </div>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<script type="text/javascript">

    $(function () {
        // 初始化时间插件
        laydate.render({elem: '#reportDate'});
        laydate.render({elem: '#expireDate'});
    });

    //保存提交的数据
    function saveReport() {
        if ($("#reportFrom").valid()) {

            //有效日期不能晚于今天
            var expireDate = new Date($("#expireDate").val());
            let now = new Date();
            if (expireDate < new Date()) {
                Dialog.errorMsg("有效日期不能晚于今天！");
                return;
            }

            //报告日期不能早于今天
            var reportDate = new Date($("#reportDate").val());
            if (reportDate > new Date()) {
                Dialog.errorMsg("报告日期不能早于今天！");
                return;
            }

            //测试报告编号 原测试报告编号不能相同
            if ($("#originalReportCode").text() == $("#reportCode").val().trim()) {
                Dialog.errorMsg("测试报告编号不能与原测试报告编号相同！");
                return;
            }

            //验证测试报告编号存不存在,true存在，false不存在
            var flag = true;
            if ($("#reportCode").val()) {
                $.ajax({
                    url: ctxPath + "/reportRepository/checkReportCode",
                    data: {'reportCode': $("#reportCode").val().trim()},
                    dataType: 'JSON',
                    async: false,
                    success: function (data) {
                        layer.closeAll();
                        if (data.result == 0) {
                            //测试报告编号不存在
                            flag = false;
                        }
                    }
                });
            }

            if (flag) {
                Dialog.errorMsg("测试报告编号已存在，请重新输入！");
                return;
            }

            var formData = new FormData($('#reportFrom')[0]);
            $.ajax({
                url: ctxPath + "/reportRepository/saveReport",
                type: 'POST',
                data: formData, // 上传formdata封装的数据包
                dataType: 'JSON',
                cache: false, // 不缓存
                processData: false, // jQuery不要去处理发送的数据
                contentType: false, // jQuery不要去设置Content-Type请求头
                async: false,
                success: function (data) {
                    layer.closeAll();
                    if (data.code == 0) {
                        Dialog.msg("操作成功!");
                        parent.refresh();
                        Dialog.closeFrame(window.name);
                    } else {
                        Dialog.errorMsg("操作失败，请重试!");
                    }
                }
            });
        }
    }

    /**
     **根据id下载报告文件
     **
     **/
    function dowloadFileById(obj) {
        var id = $(obj).first().attr("value");
        window.location.href = ctxPath + "/reportRepository/downloadFileById?id=" + id;
    }

</script>

</body>
</html>